package com.example.helloworld.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.helloworld.entity.User;
import com.example.helloworld.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;
    @GetMapping("/user")
    public List<User> query() {

        List<User> list = userMapper.selectList(null);
        System.out.println(list);
        return list;
    }

    @PostMapping("/user")
    public String save(User user){
        int i = userMapper.insert(user);
        System.out.println(user);
        if(i > 0){
            return "插入成功";
        }else {
            return "插入失败";
        }
    }

    @GetMapping("/user/findAll")
    public List<User> find(){
        return userMapper.selectAllUserAndOrders();
    }

    // 条件查询
    @GetMapping("/user/find")
    public List<User> findByCond(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username","lucy");
        return userMapper.selectList(wrapper);
    }

    // 分页查询
    @GetMapping("/user/findByPage")
    public IPage<User> findByPage(){
        Page<User> page = new Page<>(0,2);
        return userMapper.selectPage(page, null);
    }

}